package dht.run;

import java.net.InetAddress;

import dht.IServer;
import dht.entity.GlobalVariables;
import dht.entity.NodeInfo;
import dht.server.MetaServerImpl;
import dse.IOUtil;
import dse.modules.eln.EarthLikeNetwork;
import dse.modules.eln.NodeWithCoordinates;

public class MetaServer {

	public void runMetaServer(String configFile) throws Exception {

		GlobalVariables.initialize(configFile);

		NodeInfo masterInfo = new NodeInfo();
		masterInfo.setIp(InetAddress.getByName(GlobalVariables.masterIp)
				.getHostAddress());
		masterInfo.setPort(GlobalVariables.masterPort);

		EarthLikeNetwork network = IOUtil
				.NetworkFromFile(GlobalVariables.networkFile);
		int nodeId = Math.abs(masterInfo.hashCode()) % network.maxNodeId() + 1;
		masterInfo.setX(((NodeWithCoordinates) network.lookupNode(nodeId))
				.getX());
		masterInfo.setY(((NodeWithCoordinates) network.lookupNode(nodeId))
				.getY());

		IServer metaServer = new MetaServerImpl(masterInfo);
		metaServer.startServer();
	}

	public static void main(String args[]) throws Exception {
		MetaServer ms = new MetaServer();
		ms.runMetaServer(args[0]);

	}
}
